//
//  ViewController.m
//  UI0_UITableView_plist 文件
//
//  Created by wyx on 16/3/22.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "TableViewOfContact.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
//获取字典中的所有 Keys
@property (nonatomic, retain) NSArray *arrOfKeys;
@property (nonatomic, retain) NSDictionary *dic;

@end

@implementation ViewController
- (void)dealloc{
    [_arrOfKeys release];
    [_dic release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"title";
    [self createTableView];
    [self handleDate];
}
#pragma mark - 知识点1: plist文件使用
- (void)handleDate {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Contacts" ofType:@"plist"];
    //读取 plist文件(从磁盘读到内存)
    self.dic = [NSDictionary dictionaryWithContentsOfFile:path];
    //NSLog(@"%@", dic);
    //处理数据 将 Key 值进行升序排序
    self.arrOfKeys = [[self.dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    //NSLog(@"%@", self.arrOfKeys);
}
#pragma mark - createTableView
- (void)createTableView{
    TableViewOfContact *tableView = [[TableViewOfContact alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    [tableView release];
    //指定代理人
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorColor = [UIColor redColor];
}
#pragma mark - 协议方法
/** 返回 section(分区)数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arrOfKeys.count;
}
/** 根据 section 返回行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [self.arrOfKeys objectAtIndex:section];
    NSArray *arr = [self.dic objectForKey:key];
    return arr.count;
}
/** cell赋值部分 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *pool1 = @"pool1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pool1];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:pool1]autorelease];
    }
    //cell 赋值
    //找到 Key
    NSString *key = [self.arrOfKeys objectAtIndex:indexPath.section];
    //根据 Key 找到对应数组
    NSArray *arr = [self.dic objectForKey:key];
    //找到联系人小字典
    NSDictionary *dic = [arr objectAtIndex: indexPath.row];
    cell.textLabel.text = [dic objectForKey:@"name"];
    cell.detailTextLabel.text = [dic objectForKey:@"phone"];
    return cell;
}
/** 右侧索引 */
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.arrOfKeys;
}
/** cell 头部标题 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.arrOfKeys objectAtIndex:section];
}
#pragma mark - tableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //push 推出界面
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
